﻿using Application.MainModule.BranchesManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.Branches;
using Domain.MainModule.Entities;
using System.Collections.Generic;
using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for BranchManagementServiceTest and is intended
    ///to contain all BranchManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BranchManagementServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for AddBranch
        ///</summary>
        [TestMethod()]
        public void AddBranch_Invoke_Test()
        {           
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
            Guid branchId = Guid.NewGuid();
            Branch branch = new Branch()
            {
                BranchId = branchId
            };

            //Act
            target.AddBranch(branch);

            //Assert
            Branch result = target.FindBranchByBranchId(branchId);
            Assert.IsNotNull(result);
            Assert.AreEqual(branchId, result.BranchId);
        }

        /// <summary>
        ///A test for ChangeBranch
        ///</summary>
        [TestMethod()]
        public void ChangeBranch_Invoke_Test()
        {
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
            Guid branchId = Guid.Parse("ABCDFABC-1234-5678-9000-1234567890EF");
            string oldName;
            Branch originalBranch;
            Branch modifiedBranch;

            //Act
            originalBranch = target.FindBranchByBranchId(branchId);
            oldName = originalBranch.Name;
            originalBranch.Name = oldName + " Name Added";
            target.ChangeBranch(originalBranch);

            //Assert
            modifiedBranch = target.FindBranchByBranchId(branchId);
            Assert.IsNotNull(modifiedBranch);
            Assert.AreEqual(oldName + " Name Added", modifiedBranch.Name);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveBranch_Invoke_NullItemThrowNewArgumentNullException_Test()
        {
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();

            //Act
            target.RemoveBranch(null);
        }

        /// <summary>
        ///A test for FindBranchByBranchId
        ///</summary>
        [TestMethod()]
        public void FindBranchByBranchId_Invoke_Test()
        {
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
            Guid branchId = Guid.Parse("ABCDFABC-1234-5678-9000-1234567890EF");
            Branch branch = null;

            //Act
            branch = target.FindBranchByBranchId(branchId);

            //Assert
            Assert.IsNotNull(branch);
            Assert.AreEqual(branchId, branch.BranchId);
        }

        /// <summary>
        ///A test for FindBranches
        ///</summary>
        [TestMethod()]
        public void FindBranches_Invoke_Test()
        {
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
            Guid groupBranchId = Guid.Parse("012DFABC-1234-5678-9000-1234567890EF");
            List<Branch> branches = null;
            
            //Act
            branches = target.FindBranches(groupBranchId);

            //Assert
            Assert.IsNotNull(branches);
            Assert.AreEqual(2, branches.Count);
        }

        /// <summary>
        ///A test for FindPagedBranches
        ///</summary>
        [TestMethod()]
        public void FindPagedBranches_Invoke_Test()
        {
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
            List<Branch> branches = null;

            //Act
            branches = target.FindPagedBranches(0, 3);
            
            //Assert
            Assert.IsNotNull(branches);
            Assert.AreEqual(3, branches.Count);
        }

        /// <summary>
        ///A test for RemoveBranch
        ///</summary>
        [TestMethod()]
        public void RemoveBranch_Invoke_Test()
        {
            //Arrange
            IBranchManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
            Guid branchId = Guid.NewGuid();
            Branch result;
            Branch branch = new Branch()
            {
                BranchId = branchId
            };
            target.AddBranch(branch);

            //Act
            target.RemoveBranch(branch);

            //Assert
            result = target.FindBranchByBranchId(branchId);
            Assert.IsNull(result);
        }
    }
}
